package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.ClientDevelopmentPlan;
import cn.wolfcode.crm.query.ClientDevelopmentPlanObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IClientDevelopmentPlanService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("clientDevelopmentPlan")
public class ClientDevelopmentPlanController {
    @Autowired
    private IClientDevelopmentPlanService clientDevelopmentPlanService;

    @RequiresPermissions("clientDevelopmentPlan:index")
    @PermissionName("客户开发计划主页")
    @RequestMapping("")
    public String list() {

        return "clientDevelopmentPlan/list";
    }
    @RequiresPermissions("clientDevelopmentPlan:list")
    @PermissionName("客户开发计划列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(Model model, @ModelAttribute("qo") ClientDevelopmentPlanObjectQuery qo) {
//        List<ClientDevelopmentPlan> list = clientDevelopmentPlanService.getAll();
        PageResult<ClientDevelopmentPlan> list = clientDevelopmentPlanService.query(qo);
        return list;
    }

    //查找所有客户开发计划

    @RequiresPermissions("clientDevelopmentPlan:selectAll")
    @PermissionName("查询所有客户开发计划")
    @RequestMapping("selectAll")
    @ResponseBody
    public List<ClientDevelopmentPlan> selectAll() {
        List<ClientDevelopmentPlan> list = clientDevelopmentPlanService.getAll();
        return list;
    }

    @RequiresPermissions("clientDevelopmentPlan:delete")
    @PermissionName("客户开发计划删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (id != null) {
                clientDevelopmentPlanService.delete(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("删除客户开发计划失败");
        }
        return jsonResult;
    }



    @RequiresPermissions("clientDevelopmentPlan:saveOrUpdate")
    @PermissionName("客户开发计划创建更新")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(ClientDevelopmentPlan clientDevelopmentPlan) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (clientDevelopmentPlan.getId() != null) {
                clientDevelopmentPlanService.update(clientDevelopmentPlan);
            } else {
                clientDevelopmentPlanService.save(clientDevelopmentPlan);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("保存或更新客户开发计划失败");
        }
        return jsonResult;
    }


}
